package cn.edu.sgu.www.mhxysy.service.fabao.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.fabao.FabaoSixiangAttributeCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.fabao.FabaoSixiangAttributeCategory;
import cn.edu.sgu.www.mhxysy.mapper.fabao.FabaoSixiangAttributeCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.fabao.FabaoSixiangAttributeCategoryPager;
import cn.edu.sgu.www.mhxysy.service.fabao.FabaoSixiangAttributeCategoryService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author heyun沐雨橙风ιεlin
 * @version 1.0
 */
@Service
public class FabaoSixiangAttributeCategoryServiceImpl implements FabaoSixiangAttributeCategoryService {

    private final FabaoSixiangAttributeCategoryMapper fabaoSixiangAttributeCategoryMapper;

    @Autowired
    public FabaoSixiangAttributeCategoryServiceImpl(FabaoSixiangAttributeCategoryMapper fabaoSixiangAttributeCategoryMapper) {
        this.fabaoSixiangAttributeCategoryMapper = fabaoSixiangAttributeCategoryMapper;
    }

    @Override
    public void insert(FabaoSixiangAttributeCategoryDTO fabaoSixiangAttributeCategoryDTO) {
        FabaoSixiangAttributeCategory fabaoSixiangAttributeCategory = toEntity(fabaoSixiangAttributeCategoryDTO);

        fabaoSixiangAttributeCategory.setId(null);

        fabaoSixiangAttributeCategoryMapper.insert(fabaoSixiangAttributeCategory);
    }

    @Override
    public void updateById(FabaoSixiangAttributeCategoryDTO fabaoSixiangAttributeCategoryDTO) {
        ObjectUtils.requireNonNull(fabaoSixiangAttributeCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        FabaoSixiangAttributeCategory fabaoSixiangAttributeCategory = toEntity(fabaoSixiangAttributeCategoryDTO);

        fabaoSixiangAttributeCategoryMapper.updateById(fabaoSixiangAttributeCategory);
    }

    @Override
    public Page<FabaoSixiangAttributeCategory> selectByPage(FabaoSixiangAttributeCategoryPager pager) {
        QueryWrapper<FabaoSixiangAttributeCategory> queryWrapper = new QueryWrapper<>();
        Page<FabaoSixiangAttributeCategory> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getFabaoId() != null, 
                "fabao_id", pager.getFabaoId()
        );
        
        return fabaoSixiangAttributeCategoryMapper.selectPage(page, queryWrapper);
    }

    private FabaoSixiangAttributeCategory toEntity(FabaoSixiangAttributeCategoryDTO fabaoSixiangAttributeCategoryDTO) {
        FabaoSixiangAttributeCategory fabaoSixiangAttributeCategory = new FabaoSixiangAttributeCategory();

        fabaoSixiangAttributeCategory.setId(fabaoSixiangAttributeCategoryDTO.getId());
        fabaoSixiangAttributeCategory.setType(fabaoSixiangAttributeCategoryDTO.getType());
        fabaoSixiangAttributeCategory.setEffect(fabaoSixiangAttributeCategoryDTO.getEffect());
        fabaoSixiangAttributeCategory.setFabaoId(fabaoSixiangAttributeCategoryDTO.getFabaoId());

        return fabaoSixiangAttributeCategory;
    }

}